package cn.gdcp.lkqapp03_2;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
    private WebView my_WebView;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        my_WebView = findViewById(R.id.my_webview);

        my_WebView.getSettings().setJavaScriptEnabled(true);
        my_WebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        my_WebView.loadUrl("file:///android_asset/test.html");

        my_WebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                Log.e("MainActivity","message = " + message);
                Uri uri = Uri.parse(message);
                if (uri.getScheme().equals("js")) {
                    if (uri.getAuthority().equals("jumpActivityB")) {
                        String id = uri.getQueryParameter("id");
                        Intent intent = new Intent(MainActivity.this, ActivityB.class);
                        intent.putExtra("id", id);
                        startActivity(intent);
                    }
                }
                return super.onJsAlert(view, url, message, result);
            }

            @Override
            public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
                return super.onJsConfirm(view, url, message, result);
            }

            @Override
            public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
                Log.e("MainActivity","message = " + message);
                Uri uri = Uri.parse(message);
                if (uri.getScheme().equals("js")) {
                    if (uri.getAuthority().equals("getName")) {
                        final EditText editText = new EditText(MainActivity.this);
                        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                                .setTitle("请输入姓名")
                                .setView(editText)
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        name = editText.getText().toString();
                                        result.confirm(name);
                                        dialog.cancel();
                                    }
                                }).create();
                        alertDialog.show();
                    }
                }
                return true;
            }
        });

        my_WebView.setWebViewClient(new WebViewClient(){

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.e("MainActivity","url = " + url);
                Uri uri = Uri.parse(url);
                if (uri.getScheme().equals("js")){
                    if (uri.getAuthority().equals("webview")){
                        Set<String> paramSet = uri.getQueryParameterNames();
                        for (String param: paramSet){
                            String value = uri.getQueryParameter(param);
                            Log.e("MainActivity","param = " + param + "value = " +value);

                        }
                    }
                }

                view.loadUrl(url);
                return true;
                /*return super.shouldOverrideUrlLoading(view, url);*/
            }
        });

    }
}
